package com.mayuhao.homework.chapter3.homework3四2_自定义对话框;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.mayuhao.homework.R;

public class CommonDialog extends Dialog implements View.OnClickListener {
    private TextView tvTitle, tvMessage ;
   private Button tvCancel, tvConfirm;
    private String title;
    private String message;
    private String cancel;
    private String confirm;
    private MyCancelListener myCancelListener;
    private MyConfirmListener myConfirmListener;

    public CommonDialog(@NonNull Context context) {
        super(context);
    }

    public CommonDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public CommonDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public CommonDialog setMessage(String message) {
        this.message = message;
        return this;
    }

    public CommonDialog setCancel(String cancel, MyCancelListener listener) {
        this.cancel = cancel;
        this.myCancelListener = listener;
        return this;
    }

    public CommonDialog setConfirm(String confirm, MyConfirmListener listener) {
        this.confirm = confirm;
        this.myConfirmListener = listener;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog);

        tvTitle = findViewById(R.id.tv_title);
        tvMessage = findViewById(R.id.tv_message);
        tvCancel = findViewById(R.id.btn_cancel);
        tvConfirm = findViewById(R.id.btn_confirm);

        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
        }
        if (!TextUtils.isEmpty(message)) {
            tvMessage.setText(message);
        }
        if (!TextUtils.isEmpty(cancel)) {
            tvCancel.setText(cancel);
        }
        if (!TextUtils.isEmpty(confirm)) {
            tvConfirm.setText(confirm);
        }
        tvCancel.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                if (myCancelListener != null) {
                    myCancelListener.onCancel(this);
                }
                dismiss();
                break;
            case R.id.btn_confirm:
                if (myConfirmListener != null) {
                    myConfirmListener.onConfirm(this);
                }
                dismiss();
                break;
        }
    }

    public interface MyCancelListener {
        void onCancel(CommonDialog dialog);
    }

    public interface MyConfirmListener {
        void onConfirm(CommonDialog dialog);
    }
}
